package com.lesama.collections;

/*
    Collections工具类：
        public static<T> boolean addAll(Collection<? super T> c, T... elements): 给集合对象批量添加元素
        public static void shuffle(List<?> list): 打乱List集合元素的顺序
        public static<T> int binarySearch(List<T> list, T key): 以二分查找法查找元素
        public static<T> void max/min(Collection<T> coll): 根据默认的自然排序获取最大/小值
        public static<T> void swap(List<?> list, int i, int j): 交换集合中指定位置的元素
 */

import com.lesama.domain.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list, 1, 2, 3, 4, 5);
        System.out.println(list);
        System.out.println("----------------------");
        System.out.println(Collections.binarySearch(list, 3));
        System.out.println("----------------------");
        Collections.shuffle(list);
        System.out.println(list);
        System.out.println("----------------------");
        System.out.println(Collections.max(list, ((o1, o2) -> o1 - o2)));
        System.out.println(Collections.min(list, ((o1, o2) -> o1 - o2)));
        System.out.println("----------------------");
        Collections.swap(list, 1, 3);
        System.out.println(list);
        List<Student> students = new ArrayList<>();
        Collections.addAll(students, new Student("汐汐", 16), new Student("椿", 17), new Student("菲比", 16));
        System.out.println(Collections.max(students, ((o1, o2) -> o1.getAge() - o2.getAge())));
        System.out.println(Collections.min(students, ((o1, o2) -> o1.getAge() - o2.getAge())));
    }
}
